<template>
  <div>
    <el-tabs v-model="activeName" style="width: 581px">
      <el-tab-pane label="基本设置" name="first">
        <el-form ref="form" :rules="rules" :model="step" label-width="150px">
          <el-form-item label="步骤名称" prop="name">
            <el-col :span="12">
            <el-input v-model="step.name" placeholder="请输入步骤名称"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="文件路径">
            <el-select v-model="filePath" :disabled="step.fileNameInField =='Y'">
              <el-option value="${fileFolder}">默认路径</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="文件名称">
            <el-col :span="12">
            <el-input v-model="fileName" :disabled="step.fileNameInField =='Y'"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="文件扩展名">
            <el-col :span="12">
            <el-input v-model="step.file.extention"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="文件名配置项">
            <el-checkbox true-label="Y" false-label="N" v-model="step.file.add_date" :disabled="step.fileNameInField =='Y'">包含日期</el-checkbox>
            <el-checkbox true-label="Y" false-label="N" v-model="step.file.add_time" :disabled="step.fileNameInField =='Y'">包含时间</el-checkbox>
          </el-form-item>
          <el-form-item label="文件名在字段中">
            <el-checkbox true-label="Y" false-label="N" v-model="step.fileNameInField"></el-checkbox>
          </el-form-item>
          <el-form-item label="文件名字段名">
            <el-select :disabled="step.fileNameInField=='N'" v-model="step.fileNameField">
              <el-option v-for="item in step.availableFields" :value="item.name"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-tab-pane>

      <el-tab-pane label="内容设置" name="second">

        <el-form ref="form1"  label-width="150px">

          <el-form-item label="分隔符">
            <el-col :span="12">
            <el-input v-model="step.separator"></el-input>
            </el-col>
          </el-form-item>

          <el-form-item label="封闭符">
            <el-col :span="12">
              <el-input v-model="step.enclosure"></el-input>
            </el-col>
            <el-col :span="10" :offset="1">
              <el-checkbox true-label="Y" false-label="N" v-model="step.enclosure_forced">强制封闭</el-checkbox>
            </el-col>
          </el-form-item>

          <el-form-item label="换行格式">
            <el-select v-model="step.format">
              <el-option value="DOS">CR+LF(Windows, DOS)</el-option>
              <el-option value="UNIX">LF（Linux，Unix）</el-option>
            </el-select>
          </el-form-item>

        </el-form>
      </el-tab-pane>

      <el-tab-pane label="输出设置" name="three">
        <el-form ref="form2" label-width="150px">
          <el-row type="flex" justify="end" style="margin-bottom: 10px">
            <el-col :span="5">
              <el-button icon="plus" @click="addOutPut">添加输出</el-button>
            </el-col>
          </el-row>
          <el-table :data="step.fields" stripe border :max-height="250">
            <el-table-column type="index" :width="59"></el-table-column>
            <el-table-column label="名称" :width="120">
              <template scope="scope">
                <el-select v-model="scope.row.name" size="mini">
                  <el-option v-for="item in step.availableFields" :value="item.name"></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="类型" :width="150">
              <template scope="scope">
                <el-select v-model="scope.row.type" size="mini">
                  <el-option v-for="item in inputType" :value="item"></el-option>
                </el-select>
              </template>
            </el-table-column>

            <el-table-column label="格式化" :width="180">
              <template scope="scope">
                <el-select v-model="scope.row.format" size="mini">
                  <el-option v-for="item in format[scope.row.type]||['']" :value="item"></el-option>
                </el-select>
              </template>
            </el-table-column>

            <el-table-column label="空格处理" :width="120">
              <template scope="scope">
                <el-select v-model="scope.row.trim_type" size="mini">
                  <el-option v-for="item in trimType" :value="item.value" :label="item.name"></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="控制" :width="80">
              <template scope="scope">
                <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </el-tab-pane>

    </el-tabs>
  </div>
</template>

<script>
  import {Steps, Fields, Options} from "../../model/DataModel";
  import StepCommon from "./mixins/StepMixin";

  export default{
    mixins: [StepCommon],
    mounted(){
      let fileName = this.step.file.name;
      if (fileName && fileName != "") {
        let index = fileName.indexOf("}");
        this.filePath = fileName.substring(0, index + 1);
        this.fileName = fileName.substring(index + 1);
      }
    },
    watch: {
      'filePath': function (newValue) {
        this.setFileName();
      },
      'fileName': function (newValue) {
        this.setFileName();
      },
      'step.fileNameInField':function (newValue) {
        if(newValue=='Y'){
            this.filePath="";
          this.fileName="";
          this.step.file.add_date="N";
          this.step.file.add_time="N";
        }
      }
    },
    data(){
      return {
        activeName: "first",
        step: Steps.TextFileOutput(),
        filePath: "",
        fileName: "",
        trimType: Options.trimType,
        inputType: Options.inputTypes,
        format: Options.format
      }
    },
    methods: {
      setFileName(){
        this.step.file.name = this.filePath + this.fileName;
      },
      addOutPut(){
        this.step.fields.push(Fields.TextFileOutput());
      },
      updateFields(){
        let fields = this.step.fields;
        if (fields.length > 0) {
          this.step.outputFields = fields.map((el) => {
            return el.name
          }).filter(el => el != "");
        }
      }
    }
  }
</script>
